﻿// -----------------------------------------------------------------------
// <copyright file="AgeStrategy.cs" company="Hewlett-Packard">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Ex3.FriendSelector
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using FacebookWrapper.ObjectModel;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class AgeStrategy : IStrategy
    {
        private int m_MinAge;
        private int m_MaxAge;

        public AgeStrategy(int i_MinAge, int i_MaxAge)
        {
            this.m_MinAge = i_MinAge;
            this.m_MaxAge = i_MaxAge;
        }

        public bool IsFriendWanted(User i_friend)
        {
            try
            {
                int birthDay = int.Parse(i_friend.Birthday.Split('/')[0]);
                int birthMonth = int.Parse(i_friend.Birthday.Split('/')[1]);
                int birthYear = int.Parse(i_friend.Birthday.Split('/')[2]);

                bool isBigger = true;
                
                if (DateTime.Now.Year - birthYear < m_MinAge)
                {
                    isBigger = false;
                }
                else if ((DateTime.Now.Year - birthYear == m_MinAge) && (DateTime.Now.Month < birthMonth))
                {
                    isBigger = false;
                }
                else if ((DateTime.Now.Month == birthMonth) && (DateTime.Now.Day < birthDay))
                {
                    isBigger = false;
                }

                bool isYounger = true;

                if (DateTime.Now.Year - birthYear > m_MaxAge)
                {
                    isYounger = false;
                }
                else if ((DateTime.Now.Year - birthYear == m_MaxAge) && (DateTime.Now.Month > birthMonth))
                {
                    isYounger = false;
                }
                else if ((DateTime.Now.Month == birthMonth) && (DateTime.Now.Day > birthDay))
                {
                    isYounger = false;
                }

                return isBigger && isYounger;
            }
            catch
            {
                return false;
            }
        }
    }
}
